package cn.lanqiao.book.dao.impl;

import cn.lanqiao.book.dao.IBaseDao;
import cn.lanqiao.book.dao.IBookDao;
import cn.lanqiao.book.pojo.Book;

import java.util.List;

public class BookDaoImplI extends IBaseDao implements IBookDao {
    @Override
    public void addBook(Book book) {
        String sql = "insert into b_book(b_name,b_author,b_description,b_image,c_id) values(?,?,?,?,?)";
        update(sql, book.getB_name(), book.getB_author(),book.getB_description(),book.getB_image(),book.getC_id());
    }

    @Override
    public void deleteBook(String name) {
        String sql = "delete from b_book where b_name = ?";
        update(sql,name);
    }

    @Override
    public List<Book> queryAll() {
        String sql = "select * from b_book";
        return queryForList(Book.class, sql);
    }

    @Override
    public Book queryForOne(String name) {
        String sql = "select * from b_book where b_name = ?";
        return queryForOne(Book.class, sql,name);
    }

    @Override
    public void updateBook(Book book) {

        String sql = "update b_book set b_name =? , b_author = ? , b_description = ?,b_image=? , b_last_update_time=now(),  c_id = ? where b_Id = ?";
        update(sql, book.getB_name(), book.getB_author(), book.getB_description(),book.getB_image(), book.getC_id(),book.getB_id());

    }

    @Override
    public List<Book> search(String name) {
        String sql = "select * from b_book where b_name like ?";
        return queryForList(Book.class, sql,name);
    }

    @Override
    public List<Book> searchbyid(int id) {
        String sql = "select * from b_book where c_id = ?";
        return queryForList(Book.class, sql,id);
    }

    @Override
    public List<Book> topBook() {
        String sql = "select * from b_book order by b_like_count desc";
        return queryForList(Book.class, sql);
    }

    @Override
    public List<Book> finishbook() {
        String sql = "select * from b_book where b_state = 2";
        return queryForList(Book.class, sql);
    }
    @Override
    public List<Book> notfinishbook() {
        String sql = "select * from b_book where b_state = 1";
        return queryForList(Book.class, sql);
    }

    @Override
    public List<Book> searchbookbyab(String name) {
        String sql = "SELECT * FROM b_book where b_name like ? union SELECT * from b_book where b_author like ?";
        return queryForList(Book.class, sql,name,name);
    }

    @Override
    public Book queryForOnebybid(int id) {
        String sql = "select * from b_book where b_id = ?";
        return queryForOne(Book.class, sql,id);
    }

    @Override
    public void watchcount(int id) {
        String sql = "update b_book set b_count = b_count + 1 where b_id = ?";
        update(sql, id);
    }
    @Override
    public void likecount(int id) {
        String sql = "update b_book set b_like_count = b_like_count + 1 where b_id = ?";
        update(sql, id);
    }

}
